import "https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"; //引入第三方的类库
function randerlist() {
    console.log('aaa');
    console.log($('.select option:selected').val());
    $.ajax({
        url: 'http://10.31.155.14:81/2104_me/nike/php/alldata.php',
        dataType: 'json'
    }).done(function(data) {
        if (localStorage.getItem('localsid')) {
            let arrsid = localStorage.getItem('localsid').split(',');
            console.log(arrsid.length);
            for (let i = 0; i < arrsid.length; i++) {
                $.each(data, function(index, value) {
                    if (arrsid[i] == value.sid) {
                        let clonebox = $('.goodsItem:hidden').clone(true);
                        clonebox.attr('sid', value.sid);
                        clonebox.attr('oncePrice', value.price); //设置属性单价
                        clonebox.find('.goodsimg img').attr('src', value.url);
                        clonebox.find('.goodsTi').html(value.title);
                        clonebox.find('.g-price').html(value.price);
                        clonebox.css('display', 'flex');
                        $('.goodsList').append(clonebox); //追加
                        calc();
                    }
                });
            }
        }

    });
    //全选控制单选
    $('.allsel').on('click', function() {
        $('.goodsItem:visible').find('input:checkbox').prop('checked', $(this).prop('checked')); //将全选框的值赋值给购物车列表里面复选框。
        if ($(this).prop('checked')) {
            calc(); //统计总价
        } else {
            $('.to-subtotal').html('$' + 0);
            $('.to-price').html('$' + 0);
        }

    });
    //单选影响全选
    $('.goodsItem').on('click', '.cart-checkbox input', function() { //事件委托
        let len1 = $('.goodsItem:visible').find('input:checkbox').length; //复选框的长度
        let len2 = $('.goodsItem:visible').find('input:checked').length; //选中的长度
        if (len1 === len2) {
            $('.allsel').prop('checked', true);
        } else {
            $('.allsel').prop('checked', false);
        }
        calc(); //统计总价和数量
    });

}

//统计总价
function calc() {
    let sum = 0;
    $('.goodsItem').each(function(index, value) {
        if ($(this).find('input:checkbox').prop('checked')) {
            sum += parseFloat($(this).find('.g-price').html());
        }
    });
    $('.to-subtotal').html('$' + sum.toFixed(2));
    $('.to-price').html('$' + sum.toFixed(2));

};
//设置选择数量的价格变化函数
function listenprice() {
    let select = $('.select');
    select.on('click', function() {
        let num = $(this).find('option:selected').val();
        $(this).parents('.goodsItem').find('.g-price').html((num * $(this).parents('.goodsItem').attr('onceprice')).toFixed(2));
        calc();
    })
}
//删除商品
function deleteGoods() {
    $('.delete').on('click', function() {
        if (window.confirm('你确定要删除吗?')) {
            $(this).parents('.goodsItem').remove();
            console.log($(this).parents('.goodsItem').attr('sid'));
            delstorage($(this).parents('.goodsItem').attr('sid'));
            calc();
        }
    });
}
//删除本地存储中存储的编号
function delstorage(sid) {
    let index = null; //sid存在月arrsid里面的索引位置
    let arrsid = [];
    //将本地存储的编号获取到，存到数组中，然后删除要删除的编号，再重新覆盖到本地存储
    if (localStorage.getItem('localsid')) {
        arrsid = localStorage.getItem('localsid').split(','); //[1,2,4,3]
    }
    index = arrsid.indexOf(sid); //位置
    arrsid.splice(index, 1); //删除
    localStorage.setItem('localsid', arrsid)
}
//全删
function deleteAll() {
    $('.deleteAll').on('click', function() {
        if (window.confirm('你确定要删除吗?')) {
            if (localStorage.getItem('localsid')) {
                let arrsid = localStorage.getItem('localsid').split(',');
                console.log(arrsid.length);
                for (let i = 0; i < arrsid.length; i++) {
                    delstorage($('.goodsItem').attr('sid'));
                }
            }
            $('.goodsItem').remove();
            calc();
        }
    })
}

export {
    randerlist,
    listenprice,
    deleteGoods,
    deleteAll

}